package org.xiaoleng.cdm.entity.dto;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.xiaoleng.cdm.connections.DatabaseConnection;
import org.xiaoleng.cdm.connections.MySqlConnection;
import org.xiaoleng.cdm.connections.RedisConnection;
import org.xiaoleng.cdm.enums.JobStatus;

import java.util.UUID;

@Data
@Slf4j
public class DataJob {

    private String id;

    private String name;

    private JobConfiguration configuration;

    private JobStatus status;

    public DataJob(JobConfiguration configuration) {
        this.id = UUID.randomUUID().toString();
        this.name = configuration.getJobName();
        this.configuration = configuration;
    }

    public void start() {
        log.info("start job, id : {}", id);
        DatabaseConnection sourceConnection = new RedisConnection(configuration.getSource());
        DatabaseConnection targetConnection = new MySqlConnection(configuration.getTarget());
        sourceConnection.read(targetConnection::write);
        sourceConnection.close();
        targetConnection.close();
        log.info("finish job, id : {}", id);
    }
}
